//
//  FIAppDelegate.m
//  Magpie
//
//  Created by user on 12-8-31.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FIAppDelegate.h"

#import "FIViewController.h"
#import "FIAboutViewController.h"
#import "BCTabBarController.h"
#import "FIStringHelper.h"
#import "FIHttpAPIHelper.h"
#import "FIDataSourceFacade.h"
#import "FIRequestInterfaceActive.h"

@implementation FIAppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;

- (void)dealloc
{
    [_window release];
    [_tabBarController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    _tabBarController = [[BCTabBarController alloc] init];
    _tabBarController.viewControllers = [NSArray arrayWithObjects:
                                         [[UINavigationController alloc] initWithRootViewController:[[FIViewController alloc] initWithNibName:@"FIViewController" bundle:nil]],
                                         [[FIAboutViewController alloc] initWithNibName:@"FIAboutViewController" bundle:nil],
                                         [[FIViewController alloc] initWithNibName:@"FIViewController" bundle:nil],
                                         [[FIViewController alloc] initWithNibName:@"FIViewController" bundle:nil],
                                         [[FIViewController alloc] initWithNibName:@"FIViewController" bundle:nil],
                                         nil];
    {
        //init userid
        //激活
        NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
        NSString* uid = [userDefaults stringForKey:@"uid"];
        if(stringIsEmpty(uid)){
            detectNetworkAndTip();
            [self startLoadUID:nil];
        }else{
            [FIHttpAPIHelper setCommonParam:@"uid" value:uid];
        }
    }
    if ([[UIDevice currentDevice].systemVersion floatValue] < 6.0) {
        [self.window addSubview:self.tabBarController.view];
    } else {
        self.window.rootViewController = self.tabBarController;
    }
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

-(void) startLoadUID:(NSTimer *)timer{
	//Activation|source:|appName:|platform:|appVer:|ver:|appKey:|uid:|mobile:|date:|time:|
	[FIDataSourceFacade requestInterfaceActive:self action:@selector(onUIDLoaded:)];
}
-(void) onUIDLoaded:(id) resp{
	if (resp!=nil&&[resp isKindOfClass:[FIRequestInterfaceActive class]] && !stringIsEmpty([resp uid]))
    {
		//保存uid
		NSString* uid = [resp uid];
		setUid(uid);
		[FIHttpAPIHelper setCommonParam:@"uid" value:uid];
	} else {
		//出错了，重新请求
		[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(startLoadUID:) userInfo:nil repeats:NO];
	}
}

@end
